package edu.hrbu.spring.controller;

import edu.hrbu.spring.pojo.Dept;
import edu.hrbu.spring.service.DeptService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author 徐登宇
 */
@RequestMapping("/dept")
@Controller
public class DeptController {

    @Resource(name = "deptServiceImpl")
    DeptService deptService;

    // 展示部门信息
    @RequestMapping("/list")
    public String list(Model model) {
//        System.out.println("list....");
        List<Dept> deptList = deptService.getAllDept();
        model.addAttribute("deptList", deptList);
        return "dept/list";
    }

    // 修改部门信息
    @RequestMapping("/edit")
    public String edit(Dept dept) {
//        System.out.println("@@@dept@@@"+dept);
        deptService.edit(dept);
        return "redirect:/dept/list";
    }

    // 根据id查询部门信息
    @RequestMapping("/info/{deptId}")
    public String getInfo(Model model,
                          @PathVariable("deptId") int deptId) {
        Dept dept = deptService.getInfo(deptId);
        model.addAttribute("dept",dept);
        return "dept/info";
    }

    // 逻辑删除部门
    @RequestMapping("/del/{deptId}")
    public String del(@PathVariable("deptId") int deptId) {
        deptService.remove(deptId);
        return "redirect:/dept/list";
    }

    // 保存部门信息
    @RequestMapping("/save")
    public String save(Dept dept) {
//        System.out.println("@@@dept===>" + dept);
        deptService.save(dept);
        return "redirect:/dept/list";
    }

    // 保存部门视图
    @RequestMapping("/to/save")
    public String toSave() {
        return "dept/add";
    }

    // 入口视图（展示部门信息）
    @RequestMapping("/")
    public String protal() {
        return "redirect:/dept/list";
    }

}
